package com.net.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Category;
import com.net.model.Commodity;
import com.net.model.Resource;
import com.net.model.Role;
import com.net.service.ResourceService;
import com.net.service.RoleService;
import com.opensymphony.xwork2.ActionSupport;

@Component("roleAction")
@Scope("prototype")
public class RoleAction extends ActionSupport {
	private Role role;
	private Resource resource;
	private RoleService roleService;
	private ResourceService resourceService;
	private List<Role> role_list;
	private List<Role> role_search_list;
	private Set<Resource> resource_list;
	private int[] selectedList;
	private int[] selectedResourceList;
	private int start;
	private int limit;
	private int totalProperty;
	private boolean success;
	private String input;
	private String pw;
	private int current_selected_id;
	public String list(){
		System.out.println(start+":"+limit+"********");
		role_list = roleService.getRoles(start/limit, limit, null, null);
		totalProperty = roleService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String search(){
		System.out.println(start+":"+limit+"********"+"role name or id:" + role.getName());
		String str = "";
		if("".equals(role.getName())){
			str = "r.id="+role.getId();
		}else if(role.getId()==0){
			str = "r.name like '%"+role.getName()+"%'";
		}else {
			str = "r.name like '%"+role.getName()+"%'" + " or r.id="+role.getId();
		}
		role_list = roleService.getRolesByCondition(start/limit, limit, str);
		totalProperty = roleService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String searchResources(){
		System.out.println("current_selected_id:"+current_selected_id);
		String str = "";
			str = "r.id="+current_selected_id;
		role_list = roleService.getRolesByCondition(0,roleService.getCount(), str);//改成loadById有问题
		List<Resource> resources = new ArrayList<Resource>();
		if(role_list.size()!=0){
			resources.addAll(role_list.get(0).getResources());
			resource_list = new HashSet<Resource>();
			for(int i = start; i<start+limit&i<resources.size();i++){
				resource_list.add(resources.get(i));
			}
		}else
		role_list=null;
		totalProperty = resources.size();
		success = true;
		return SUCCESS;
	}
	
	public String delete(){
		System.out.println("selectedLista:"+selectedList[0]+"length:"+selectedList.length);
		/*for(int i = 0 ; i < selectedList.length ; i++){
			role = roleService.loadRoleById(selectedList[i]);
			//role.setResources(null);
			System.out.println(role.getName()+"********");
			roleService.delete(role);
		}*/
		
		for(int i = 0 ; i < selectedList.length ; i++){
			role = roleService.loadRoleById(selectedList[i]);
			resource_list = role.getResources();
			for(Resource resource : resource_list){
				resource.getRoles().remove(role);
				resourceService.update(resource);
			}
			//System.out.println(role.getValue()+"********");
			roleService.delete(role);
		}
		
		role = null;
		success = true;
		return SUCCESS;
	}
	
	public String deleteResources(){
		System.out.println("current_selected_id:"+current_selected_id+"length:"+selectedResourceList.length);
		for(int i = 0 ; i < selectedResourceList.length ; i++){
			role = roleService.loadRoleById(current_selected_id);
			role.getResources().remove(resourceService.loadResourceById(selectedResourceList[i]));
			System.out.println(role.getName()+"********");
			roleService.update(role);
		}
		role = null;
		success = true;
		return SUCCESS;
	}
	
	public String add(){
		HttpServletResponse response = ServletActionContext.getResponse();
		  //设置返回数据的编码为UTF-8
		  response.setCharacterEncoding("UTF-8");
		  System.out.println(role.getName()+":name");
		role.setResources(null);
		roleService.add(role);
		System.out.println(role.getId()+":role id");
		success = true;
		return SUCCESS;
	}
	
	public String addResources(){
		String str = "";
		str = "r.id="+current_selected_id;
		System.out.println("current_selected_id 888:"+current_selected_id);
		role_list = roleService.getRolesByCondition(0,roleService.getCount(), str);
		role = role_list.get(0);
		System.out.println("role.getName():"+role.getName());
		resource_list = role.getResources();
		for(int i = 0 ; i < selectedResourceList.length ; i++){
			resource = resourceService.loadResourceById((selectedResourceList[i]));
			Set<Role> roles = resource.getRoles();
			roles.add(role);
			resource.setRoles(roles);
			resourceService.update(resource);
			resource_list.add(resource);
		}
		role.setResources(resource_list);
		roleService.update(role);
		System.out.println(role.getId()+":role id");
		
		role = null;
		role_list = null;
		resource_list = null;
		resource = null;
		
		success = true;
		return SUCCESS;
	}
	
	public String update(){
		System.out.println(role.getName()+":disable");
		roleService.update(role);
		success = true;
		return SUCCESS;
	}
	
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	@JSON(serialize=false)
	public RoleService getRoleService() {
		return roleService;
	}
	@javax.annotation.Resource(name="roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotalProperty() {
		return totalProperty;
	}
	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(int[] selectedList) {
		this.selectedList = selectedList;
	}

	public List<Role> getRole_list() {
		return role_list;
	}

	public void setRole_list(List<Role> role_list) {
		this.role_list = role_list;
	}

	public List<Role> getRole_search_list() {
		return role_search_list;
	}

	public void setRole_search_list(List<Role> role_search_list) {
		this.role_search_list = role_search_list;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public int getCurrent_selected_id() {
		return current_selected_id;
	}

	public void setCurrent_selected_id(int current_selected_id) {
		this.current_selected_id = current_selected_id;
	}

	public Set<Resource> getResource_list() {
		return resource_list;
	}

	public void setResource_list(Set<Resource> resource_list) {
		this.resource_list = resource_list;
	}

	public int[] getSelectedResourceList() {
		return selectedResourceList;
	}

	public void setSelectedResourceList(int[] selectedResourceList) {
		this.selectedResourceList = selectedResourceList;
	}
	@JSON(serialize=false)
	public ResourceService getResourceService() {
		return resourceService;
	}
	@javax.annotation.Resource(name="resourceService")
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

}
